#!/bin/bash

# HTTP API 测试脚本
echo "=== HTTP API 测试 ==="

USER_SERVICE_URL="http://localhost:8081/api/v1"
ORDER_SERVICE_URL="http://localhost:8082/api/v1"

# 测试用户服务健康检查
echo "1. 测试用户服务健康检查..."
curl -s http://localhost:8081/health | jq .
echo ""

# 测试订单服务健康检查
echo "2. 测试订单服务健康检查..."
curl -s http://localhost:8082/health | jq .
echo ""

# 创建用户
echo "3. 创建用户..."
USER_RESPONSE=$(curl -s -X POST "$USER_SERVICE_URL/users" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "HTTP Test User",
    "email": "httptest@example.com"
  }')

echo "$USER_RESPONSE" | jq .
USER_ID=$(echo "$USER_RESPONSE" | jq -r '.id')
echo "创建的用户ID: $USER_ID"
echo ""

# 获取用户信息
echo "4. 获取用户信息..."
curl -s "$USER_SERVICE_URL/users/$USER_ID" | jq .
echo ""

# 根据邮箱获取用户
echo "5. 根据邮箱获取用户..."
curl -s "$USER_SERVICE_URL/users/by-email?email=httptest@example.com" | jq .
echo ""

# 创建订单
echo "6. 创建订单..."
ORDER_RESPONSE=$(curl -s -X POST "$ORDER_SERVICE_URL/orders" \
  -H "Content-Type: application/json" \
  -d "{
    \"user_id\": $USER_ID,
    \"product\": \"HTTP Test Product\",
    \"amount\": 199.99
  }")

echo "$ORDER_RESPONSE" | jq .
ORDER_ID=$(echo "$ORDER_RESPONSE" | jq -r '.id')
echo "创建的订单ID: $ORDER_ID"
echo ""

# 获取订单信息
echo "7. 获取订单信息..."
curl -s "$ORDER_SERVICE_URL/orders/$ORDER_ID" | jq .
echo ""

# 获取用户的所有订单
echo "8. 获取用户的所有订单..."
curl -s "$ORDER_SERVICE_URL/orders/user/$USER_ID" | jq .
echo ""

# 更新订单状态
echo "9. 更新订单状态..."
curl -s -X PUT "$ORDER_SERVICE_URL/orders/$ORDER_ID/status" \
  -H "Content-Type: application/json" \
  -d '{"status": "completed"}' | jq .
echo ""

# 再次获取订单信息确认状态更新
echo "10. 确认订单状态更新..."
curl -s "$ORDER_SERVICE_URL/orders/$ORDER_ID" | jq .
echo ""

echo "=== HTTP API 测试完成 ==="
